package com.gsq.zjcgj.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.gsq.zjcgj.R;
import com.gy.mbaselibrary.utils.ScreenUtil;
import com.gy.mbaselibrary.utils.StringUtil;


/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/2/6
 * 修改人：
 * 修改时间：2018/2/6
 * 修改备注：
 */
public class LoadingDialog extends Dialog {
    private Animation animation;
    private ImageView iv_image;
    private TextView tv_text;


    public LoadingDialog(Context context) {
        super(context);
        init();
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loading,null);
        setContentView(view);
        iv_image = view.findViewById(R.id.iv_image);
        tv_text = view.findViewById(R.id.tv_text);
        setCancelable(false);//点击外部不可dismiss
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setDimAmount(0.0f);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtil.dp2px(150,getContext());
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                iv_image.clearAnimation();
                animation = null;
            }
        });
    }

    @Override
    public void show() {
        this.show(null);
    }

    public void show(String str){
        if (!isShowing()){
            super.show();
            animation = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_base);
            iv_image.setAnimation(animation);
        }
        if (StringUtil.isEmpty(str)){
            if (tv_text.getVisibility() == View.VISIBLE){
                tv_text.setVisibility(View.GONE);
            }
        }else {
            tv_text.setText(str);
            if (tv_text.getVisibility() == View.GONE){
                tv_text.setVisibility(View.VISIBLE);
            }
        }


    }


}
